- Layer 1: (21 inputs × 20 neurons) + 20 biases = 440 parameters
- Output Layer: (20 inputs × 1 neuron) + 1 bias = 21 parameters
- Total: 461 parameters
This unit brings together two important topics in machine learning. First, we wrap up our discussion on Neural Networks with a practical Python demonstration on the Diabetes dataset, exploring various hyperparameters and regularization techniques. Second, we introduce the vast field of Clustering, specifically focusing on hierarchical approaches with Agglomerative Clustering and its linkage methods: single, complete, and average.
Neural networks are powerful models but come with important tradeoffs. The most prominent advantage is their good predictive performance. They are known to have high tolerance to noisy data and the ability to capture highly complicated non-linear relationships between predictors and an outcome variable.
Their weakest point is in providing insight into the structure of the relationship, hence their blackbox reputation. Several considerations and dangers should be kept in mind when using neural networks:
The demo uses the Diabetes health indicator dataset from UCI ML Repository containing 21 columns and tens of thousands of records. Predictors include HighBP, HighChol, Stroke, Smoking, Fruits, Veges, Age, Sex, BMI, Education, Income, etc. The target has two categories: 0 for no diabetes, 1 for pre-diabetes or diabetes.
Dense(20, relu, input_dim=21) + Dropout(0.25) + Dense(1, sigmoid). How many parameters in this architecture?
Result with early stopping on val_loss: [loss=0.3150, AUC=0.8287]
| Model Name | Time (sec) | AUC |
|---|---|---|
| LGBM | 0.540 | 0.8297 |
| Cat Boost | 1.799 | 0.8289 |
| XGB | 1.468 | 0.8296 |
| MLP (sklearn, 10 epochs) | 5.900 | 0.8279 |
Cluster Analysis is about finding similarities between data and grouping similar data objects into clusters. It is an unsupervised learning method: no predefined classes are used. A canonical example is Google News grouping similar news stories.
Depending on your viewpoint, the same dataset could have 4 clusters, 2 clusters, or 6 clusters. The "right" answer depends on context and application. This is one reason clustering evaluation is subtle.
| Aspect | Hierarchical Clustering | Partitional Clustering (e.g., K-Means) |
|---|---|---|
| Number of Clusters | No need to specify in advance | Some require K (K-Means, K-Medoids); some discover K (DBSCAN, OPTICS, ART) |
| Result | Dendrogram showing nested clusters | Single partition of data |
| Flexibility | Can obtain any number of clusters by cutting dendrogram | Fixed K clusters |
| Dataset Size | Best for small to medium datasets (< 10,000 points) | Suitable for large datasets (millions of points) |
| Common Applications | Biological taxonomy, document organization, gene sequence analysis, social network analysis | Customer segmentation, image compression, document clustering, anomaly detection |
| Deterministic? | Yes — same data gives same result | Varies: K-Means no, DBSCAN yes |
The key operation is the computation of the proximity of two clusters. Different approaches to defining this distance distinguish the different Agglomerative algorithms.
Given two clusters \( c_i \) and \( c_j \), how do we compute a single distance \( D(c_i, c_j) \) between them? Four common methods:
Definition: Distance between clusters = the shortest distance between any two points in different clusters.
When merging \( c_k = c_i \cup c_j \), the Lance-Williams update is:
Susceptible to chaining — single long bridge can merge whole chains of clusters.
Definition: Distance between clusters = the greatest distance between any two points in different clusters.
Produces compact, tightly-diameter clusters; sensitive to outliers.
Definition: Distance between clusters = the average distance between all pairs of points in different clusters.
Update formula for \( c_k = c_i \cup c_j \):
Balanced compromise between single and complete linkage.
Definition: Distance between clusters = Euclidean distance between their cluster centroids (mean vectors).
Simple but can suffer from inversions (merging increases total distance).
An MLP has: Input dim = 20, Hidden1 = 32 (ReLU), Hidden2 = 16 (ReLU), Output = 3 (softmax). How many trainable parameters are there?
In Pass 5 we compared Adam vs SGD and observed that:
Which statement is MOST supported by this single comparison?
On this specific dataset, with this architecture, Adam achieves a higher test AUC than SGD with momentum 0.9 at lr = 0.01. We cannot generalize this to all datasets or architectures — SGD may outperform Adam on other tasks, especially with tuning.
A startup with 100,000,000 customer records wants to run marketing on customer segments. Which clustering approach is better, and why?
Partitional clustering (K-Means / DBSCAN). Hierarchical clustering has \(O(n^2)\) memory cost for the proximity matrix, which is impossible for 100M points. Partitional methods scale linearly or near-linearly. The flexibility of choosing K post-hoc via dendrogram is not worth the computational infeasibility here.
Two clusters are shaped like two long thin crescents that touch at one point. Which linkage method will definitely merge them first?
Single Linkage (MIN). Because the touching pair has distance ≈ 0, Single Linkage will merge the crescents even if the majority of points in each crescent are far apart. This is the "chaining effect." Complete Linkage would compute the MAX distance (crescent tip to opposite tip) and keep them separate.
Given 5 points with Euclidean distance matrix:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 3 | 7 | 9 | 11 |
| B | 3 | 0 | 6 | 8 | 10 |
| C | 7 | 6 | 0 | 2 | 12 |
| D | 9 | 8 | 2 | 0 | 13 |
| E | 11 | 10 | 12 | 13 | 0 |
Which pair is merged first in Single Linkage? What is the merge distance?
Step 1. Find the smallest non-zero entry in the matrix.
Off-diagonal values: A-B=3, A-C=7, A-D=9, A-E=11, B-C=6, B-D=8, B-E=10, C-D=2, C-E=12, D-E=13.
Step 2. Minimum value = 2, between C and D.
Step 3. Single Linkage uses min-distance, merge criterion is the smallest entry.
➡️ First merge: {C, D} at distance 2.
After merging C and D from Problem 1 into cluster CD = {C, D}, compute the distance between CD and the other points (A, B, E) using Complete Linkage (MAX).
Step 1. D(CD, A) = max(d(C,A), d(D,A)) = max(7, 9) = 9
Step 2. D(CD, B) = max(d(C,B), d(D,B)) = max(6, 8) = 8
Step 3. D(CD, E) = max(d(C,E), d(D,E)) = max(12, 13) = 13
New distances:
| CD | A | B | E | |
|---|---|---|---|---|
| CD | 0 | 9 | 8 | 13 |
| A | 9 | 0 | 3 | 11 |
| B | 8 | 3 | 0 | 10 |
| E | 13 | 11 | 10 | 0 |
Next merge will be A-B at distance 3 (Complete Linkage).
Cluster X = {p1, p2} and Cluster Y = {q1, q2, q3}. The matrix of pairwise Euclidean distances is:
| q1 | q2 | q3 | |
|---|---|---|---|
| p1 | 2 | 4 | 6 |
| p2 | 3 | 5 | 7 |
Compute the Average Linkage distance D(X, Y).
Step 1. Count pairs: |X| = 2, |Y| = 3, total pairs = 2 × 3 = 6.
Step 2. Sum all pairwise distances:
Step 3. Divide by number of pairs:
➡️ Average Linkage distance = 4.5.
A binary classification MLP takes 15 features as input. The architecture is: Input → Dense(8, relu) → Dense(4, relu) → Dense(1, sigmoid). Compute the total number of trainable parameters.
A Dropout(0.25) layer follows a Dense(20) layer. Explain:
Three 1-D points at positions: p1 = 0, p2 = 5, p3 = 9. We start with singletons {p1}, {p2}, {p3}.
1st merge: Pairwise distances: d(p1,p2)=5, d(p2,p3)=4, d(p1,p3)=9. Min is 4 → merge {p2, p3} at distance 4.
Now we have C23 = {p2, p3} and the singleton {p1}. We need D(C23, {p1}):
Your score: 0 / 5